package cn.zy.jboa.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.enterprise.inject.New;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import cn.zy.jboa.entity.BizClaimVoucher;
import cn.zy.jboa.entity.BizClaimVoucherDetail;
import cn.zy.jboa.entity.SysEmployee;
import cn.zy.jboa.service.BizClaimVoucherService;
import cn.zy.jboa.util.PageSupport;

import com.opensymphony.xwork2.ActionSupport;

public class BizClaimVoucherAction extends ActionSupport {
	
	@Autowired
	BizClaimVoucherService bizClaimVoucherService;
	
	String startDate=null;
	
	Timestamp time = new Timestamp(new Date().getTime());

	Map statusMap = new HashMap();

	String endDate=null;

	PageSupport pageSupport = new PageSupport();

	int pageNo = 1;

	BizClaimVoucher claimVoucher;

	List<BizClaimVoucherDetail> detailList = new ArrayList<BizClaimVoucherDetail>();

	public void setBizClaimVoucherService(
			BizClaimVoucherService bizClaimVoucherService) {
		this.bizClaimVoucherService = bizClaimVoucherService;
	}

	public Map getStatusMap() {
		statusMap.put("新创建", "新创建");
		statusMap.put("已提交", "已提交");
		statusMap.put("待审批", "待审批");
		statusMap.put("已打回", "已打回");
		statusMap.put("已审批", "已审批");
		statusMap.put("已付款", "已付款");
		statusMap.put("已终止", "已终止");
		return statusMap;
	}

	public void setStatusMap(Map statusMap) {
		this.statusMap = statusMap;
	}
	
	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public PageSupport getPageSupport() {
		return pageSupport;
	}

	public void setPageSupport(PageSupport pageSupport) {
		this.pageSupport = pageSupport;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public BizClaimVoucher getClaimVoucher() {
		return claimVoucher;
	}

	public void setClaimVoucher(BizClaimVoucher claimVoucher) {
		this.claimVoucher = claimVoucher;
	}

	public List<BizClaimVoucherDetail> getDetailList() {
		return detailList;
	}

	public void setDetailList(List<BizClaimVoucherDetail> detailList) {
		this.detailList = detailList;
	}

	public Timestamp getTime() {
		return time;
	}

	public void setTime(Timestamp time) {
		this.time = time;
	}

	public String getClaimVoucherById(){
		claimVoucher = bizClaimVoucherService.getId(claimVoucher.getId());
		return SUCCESS;
	}

	public String searchClaimVoucher(){
		SysEmployee employee =(SysEmployee) ServletActionContext.getRequest().getSession().getAttribute("employee");
		String position = (String) ServletActionContext.getRequest().getSession().getAttribute("employee_position");
		String status = null;
		if(claimVoucher!=null){
			status=claimVoucher.getStatus();
			if("全部".equals(status)){
				status = null;
			}
		}
		pageSupport.setPageSize(5);
		pageSupport.setCurrentPageNo(this.pageNo);
		pageSupport.setTotalCount(bizClaimVoucherService.getCount(employee.getSn(),position,startDate,endDate,status));
		pageSupport.setItems((List) bizClaimVoucherService.claimList(employee.getSn(),
				position,(pageSupport.getCurrentPageNo()-1)*pageSupport.getPageSize(),
				pageSupport.getPageSize(),startDate,endDate,status));
		ServletActionContext.getRequest().getSession().setAttribute("pageSupport", pageSupport);
		return SUCCESS;
	}
	
	
	public String deleteClaimVoucherById(){
		bizClaimVoucherService.delId(claimVoucher.getId());
		return SUCCESS;
	}
	public String updateClaimVoucher(){
		BizClaimVoucher cvs = bizClaimVoucherService.getId(claimVoucher.getId());
		cvs.setStatus(claimVoucher.getStatus());
		cvs.setEvent(claimVoucher.getEvent());
		cvs.setTotalAccount(claimVoucher.getTotalAccount());
		Timestamp date = new Timestamp(new Date().getTime());
		cvs.setModifyTime(date);
		System.out.println(detailList.size());
		Set set = new HashSet(detailList);
		for (Object obj : set) {
			BizClaimVoucherDetail a = (BizClaimVoucherDetail) obj;
			a.setBizClaimVoucher(cvs);
		}
		cvs.setBizClaimVoucherDetails(set);
		bizClaimVoucherService.updateCV(cvs);
		return SUCCESS;
	}
	
	public String toAdd(){
		return SUCCESS;
	}
	public String saveClaimVoucher(){
		claimVoucher.setCreateTime(time);
		claimVoucher.setModifyTime(null);
		claimVoucher.setSysEmployeeByCreateSn((SysEmployee) ServletActionContext.getRequest().getSession().getAttribute("employee"));
		claimVoucher.setSysEmployeeByNextDealSn((SysEmployee) ServletActionContext.getRequest().getSession().getAttribute("manager"));
		Set set = new HashSet(detailList);
		for (Object obj : set) {
			BizClaimVoucherDetail a = (BizClaimVoucherDetail) obj;
			a.setBizClaimVoucher(claimVoucher);
		}
		claimVoucher.setBizClaimVoucherDetails(set);
		bizClaimVoucherService.add(claimVoucher);
		return SUCCESS;
	}
	
}
